package com.addcn.android.design591.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.addcn.android.design591.R;
import com.addcn.android.design591.adapter.ViewpagerAdapter;
import com.addcn.android.design591.fragment.HomeFragment;
import com.addcn.android.design591.fragment.MoreFragment;
import com.addcn.android.design591.fragment.OfferFragment;
import com.addcn.android.design591.fragment.UserFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xuge on 2016/12/30.
 * 首页
 */

public class MainActivity extends AppCompatActivity {
    private Context context = this;
    private Toolbar toolbar;
    private AppBarLayout appbarlayout;
    private RecyclerView recyclerview;

    public ViewPager viewpager;
    private TabLayout tablayout;

    private List<String> tabIndicators;
    private List<Integer> tabImages;
    private List<Fragment> tabFragments;
    private ViewpagerAdapter viewpagerAdapter;
    private CoordinatorLayout coordinatorlayout;
    private NavigationView navigationview;
    private DrawerLayout drawerlayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initView();

        configViews();

        initTab();
    }

    private void initTab() {
        setSupportActionBar(toolbar);

        // 设置Drawerlayout开关指示器，即Toolbar最左边的那个icon
        ActionBarDrawerToggle mActionBarDrawerToggle =
                new ActionBarDrawerToggle(this, drawerlayout, toolbar, R.string.open, R.string.close);
        mActionBarDrawerToggle.syncState();
        drawerlayout.addDrawerListener(mActionBarDrawerToggle);

        navigationview.inflateHeaderView(R.layout.header_nav);
        navigationview.inflateMenu(R.menu.main_menu_nav);

        navigationview.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                return false;
            }
        });

        toolbar.setOverflowIcon(getResources().getDrawable(R.drawable.ic_more_vert_white_24dp));
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {

                return false;
            }
        });


        //去掉最初和最后一个滑动时的阴影
        viewpager.setOverScrollMode(View.OVER_SCROLL_NEVER);
        tablayout.setTabMode(TabLayout.MODE_FIXED);
        tablayout.setSelectedTabIndicatorHeight(0);
        ViewCompat.setElevation(tablayout, 10);
        tablayout.setupWithViewPager(viewpager);

        for (int i = 0; i < tabIndicators.size(); i++) {
            TabLayout.Tab itemTab = tablayout.getTabAt(i);
            if (itemTab != null) {
                itemTab.setCustomView(R.layout.item_tab_layout);

                ImageView itemimg = (ImageView) itemTab.getCustomView().findViewById(R.id.tablayout_img);
                itemimg.setImageResource(tabImages.get(i));

                TextView itemTv = (TextView) itemTab.getCustomView().findViewById(R.id.tablayout_text);
                itemTv.setText(tabIndicators.get(i));
            }
        }

        tablayout.getTabAt(0).getCustomView().setSelected(true);
    }

    private void configViews() {
        tabIndicators = new ArrayList<>();
        tabIndicators.add("首頁");
        tabIndicators.add("設計報價");
        tabIndicators.add("會員中心");
        tabIndicators.add("留言反饋");


        tabImages = new ArrayList<>();
        tabImages.add(R.drawable.selector_image_home);
        tabImages.add(R.drawable.selector_image_offer);
        tabImages.add(R.drawable.selector_image_user);
        tabImages.add(R.drawable.selector_image_more);

        tabFragments = new ArrayList<>();
        tabFragments.add(new HomeFragment());
        tabFragments.add(new OfferFragment());
        tabFragments.add(new UserFragment());
        tabFragments.add(new MoreFragment());


        viewpagerAdapter = new ViewpagerAdapter(getSupportFragmentManager(), tabFragments);
        viewpager.setAdapter(viewpagerAdapter);
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                toolbar.setTitle(tabIndicators.get(position));
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        appbarlayout = (AppBarLayout) findViewById(R.id.appbarlayout);
        viewpager = (ViewPager) findViewById(R.id.viewpager);
        tablayout = (TabLayout) findViewById(R.id.tablayout);
        coordinatorlayout = (CoordinatorLayout) findViewById(R.id.coordinatorlayout);
        navigationview = (NavigationView) findViewById(R.id.navigationview);
        drawerlayout = (DrawerLayout) findViewById(R.id.drawerlayout);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu_toolbar_menu, menu);
        return true;
    }

}
